package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rzdata.poct.system.domain.business.bo.PoctLocalStoreBo;
import com.rzdata.poct.system.domain.business.vo.PoctLocalStoreVo;
import com.rzdata.poct.system.domain.business.po.PoctLocalStore;
import com.rzdata.poct.system.mapper.PoctLocalStoreMapper;
import com.rzdata.poct.system.service.IPoctLocalStoreService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * poct 本地文件记录Service业务层处理
 *
 * @author fangli
 * @date 2021-09-18
 */
@Service
public class PoctLocalStoreServiceImpl extends ServicePlusImpl<PoctLocalStoreMapper, PoctLocalStore, PoctLocalStoreVo> implements IPoctLocalStoreService {

    @Override
    public PoctLocalStoreVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctLocalStoreVo> queryPageList(PoctLocalStoreBo bo) {
        PagePlus<PoctLocalStore, PoctLocalStoreVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<PoctLocalStoreVo> queryList(PoctLocalStoreBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctLocalStore> buildQueryWrapper(PoctLocalStoreBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctLocalStore> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStatus() != null, PoctLocalStore::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getFilePath()), PoctLocalStore::getFilePath, bo.getFilePath());
        lqw.like(StringUtils.isNotBlank(bo.getFileName()), PoctLocalStore::getFileName, bo.getFileName());
        lqw.eq(StringUtils.isNotBlank(bo.getFlieType()), PoctLocalStore::getFlieType, bo.getFlieType());
        lqw.eq(StringUtils.isNotBlank(bo.getCrateBy()), PoctLocalStore::getCrateBy, bo.getCrateBy());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctLocalStoreBo bo) {
        PoctLocalStore add = BeanUtil.toBean(bo, PoctLocalStore.class);
        validEntityBeforeSave(add);
        return save(add);
    }
    @Override
    public Long insertByBoId(PoctLocalStoreBo bo) {
        PoctLocalStore add = BeanUtil.toBean(bo, PoctLocalStore.class);
        validEntityBeforeSave(add);
        save(add);
        System.out.println("============"+add.getId());
        return add.getId();
    }

    @Override
    public Boolean updateByBo(PoctLocalStoreBo bo) {
        PoctLocalStore update = BeanUtil.toBean(bo, PoctLocalStore.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctLocalStore entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
